package com.rongxun.hbwallet.ui.fragment.loadwallet;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongxun.base.BaseFragment;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.constants.HttpConstants;
import com.rongxun.hbwallet.constants.IntentKey;
import com.rongxun.hbwallet.ui.activities.LoadWalletActivity;
import com.rongxun.hbwallet.ui.activities.WebActivity;
import com.rongxun.utils.ActivityUtils;
import com.rongxun.utils.ToastUtils;
import com.rongxun.utils.wallet.ETHWalletUtils;
import com.rongxun.utils.wallet.WalletDaoUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dwq on 2018/3/22/022.
 * e-mail:lomapa@163.com
 */

public class LoadWalletByMnemonicFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.et_mnemonic)
    EditText     etMnemonic;
    @BindView(R.id.et_wallet_pwd)
    EditText     etWalletPwd;
    @BindView(R.id.et_wallet_pwd_again)
    EditText     etWalletPwdAgain;
    @BindView(R.id.cb_agreement)
    CheckBox     cbAgreement;
    @BindView(R.id.tv_agreement)
    TextView     tvAgreement;
    @BindView(R.id.lly_wallet_agreement)
    LinearLayout llyWalletAgreement;
    @BindView(R.id.btn_load_wallet)
    TextView     btnLoadWallet;

    private String ethType = ETHWalletUtils.ETH_JAXX_TYPE;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_load_wallet_by_mnemonic;
    }

    LoadWalletActivity mLoadWalletActivity;

    @Override
    protected void init() {
        mLoadWalletActivity = (LoadWalletActivity) activity;

        cbAgreement.setOnCheckedChangeListener(this);
    }

    @OnClick({R.id.btn_load_wallet, R.id.tv_agreement})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_load_wallet:
                String mnemonic = etMnemonic.getText().toString().trim();
                String walletPwd = etWalletPwd.getText().toString().trim();
                String confirmPwd = etWalletPwdAgain.getText().toString().trim();
                String pwdReminder = "";
                boolean verifyWalletInfo = verifyInfo(mnemonic, walletPwd, confirmPwd, pwdReminder);
                if (verifyWalletInfo) {
                    mLoadWalletActivity.getPresenter().loadWalletByMnemonic(ethType, mnemonic, walletPwd);
                }
            case R.id.tv_agreement:
                ActivityUtils.startActivityWithAnimotion(activity, new Intent(activity, WebActivity.class).putExtra(IntentKey.WEB_URL, HttpConstants.WEB_ADDRESS.AGREEMENT_URL));
                break;

        }
    }

    private boolean verifyInfo(String mnemonic, String walletPwd, String confirmPwd, String pwdReminder) {
        if (TextUtils.isEmpty(mnemonic)) {
            ToastUtils.showToast(R.string.load_wallet_by_mnemonic_input_tip);
            return false;
        } else if (WalletDaoUtils.checkRepeatByMenmonic(mnemonic)) {
            ToastUtils.showToast(R.string.load_wallet_already_exist);
            return false;
        } else if (TextUtils.isEmpty(walletPwd)) {
            ToastUtils.showToast(R.string.create_wallet_pwd_input_tips);
            // 同时判断强弱
            return false;
        } else if (TextUtils.isEmpty(confirmPwd) || !TextUtils.equals(confirmPwd, walletPwd)) {
            ToastUtils.showToast(R.string.create_wallet_pwd_confirm_input_tips);
            return false;
        }
        return true;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        cbAgreement.setChecked(isChecked);
        btnLoadWallet.setEnabled(isChecked);
        btnLoadWallet.setTextColor(isChecked ? Color.parseColor("#D8B267") : Color.WHITE);
    }
}
